package com.bestcem.xm.qdes.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bestcem.xm.common.core.annotation.XmGet;
import com.bestcem.xm.common.core.domain.web.XmResultJson;
import com.bestcem.xm.qdes.controller.param.QuestionLibPageParam;
import com.bestcem.xm.qdes.controller.param.QuestionLibSaveAndCopyParam;
import com.bestcem.xm.qdes.controller.param.QuestionLibSetTagParam;
import com.bestcem.xm.qdes.controller.vo.DeletedCountVo;
import com.bestcem.xm.qdes.service.QdesQuestionLibService;
import com.bestcem.xm.qdes.util.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;

/**
 * 模板库-题目库
 */
@Api(value = "/qdes", tags = {"模板库-题目库"})
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/qdes/qlibs/questionlib")
public class QuestionLibController {

    private @NonNull QdesQuestionLibService questionlibService;

    /**
     * 题目库-题目详情查询/题目列表查询
     * 问卷编辑，导入题目 也请求了这个接口，但是未带分页参数
     *
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataTypeClass = Long.class, name = "page", value = "页码"),
            @ApiImplicitParam(paramType = "query", dataTypeClass = Long.class, name = "rowsPerPage", value = "每页条数")
    })
    @ApiOperation(value = "题目库-题目详情查询/题目列表查询", notes = "题目库-题目详情查询/题目列表查询", httpMethod = "GET")
    @GetMapping("/")
    public XmResultJson<Object> questionlibQuery(@ApiIgnore Page<?> page, @XmGet QuestionLibPageParam pageParam, HttpServletRequest request) {
        //相同地址，存在不带分页的请求
        if (!PageUtil.isPageQuery(request)) {
            return XmResultJson.success(questionlibService.questionlibQuery(pageParam));
        }
        return XmResultJson.success(questionlibService.questionlibPageQuery(page, pageParam));
    }

    /**
     * 题目库-题目模板保存和复制接口
     *
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataTypeClass = QuestionLibSaveAndCopyParam.class, name = "param", value = "参数")
    })
    @ApiOperation(value = "题目库-题目模板保存和复制接口", notes = "题目库-题目模板保存和复制接口", httpMethod = "POST")
    @PostMapping("/")
    public XmResultJson<Object> questionlibSaveAndCopy(@RequestBody @Validated QuestionLibSaveAndCopyParam param, BindingResult result) {
        if (result.hasErrors()) {
            return XmResultJson.fail(1, StrUtil.join(";", result.getAllErrors()));
        }
        questionlibService.questionlibSaveAndCopy(param);
        return XmResultJson.success(null, Boolean.TRUE.equals(param.getWithCopy()) ? "复制成功" : "创建成功");
    }

    /**
     * 题目库-题目模板设置标签
     *
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataTypeClass = String.class, name = "id", value = "主键"),
            @ApiImplicitParam(paramType = "body", dataTypeClass = QuestionLibSetTagParam.class, name = "param", value = "参数")
    })
    @ApiOperation(value = "题目库-题目模板设置标签", notes = "题目库-题目模板设置标签", httpMethod = "PUT")
    @PutMapping("/{id:[a-fA-F0-9]{24}}")
    public XmResultJson<Object> questionlibSetTag(@PathVariable String id, @RequestBody QuestionLibSetTagParam param) {
        questionlibService.questionlibSetTag(id, param);
        return XmResultJson.success(null, "设置成功");
    }

    /**
     * 题目库-题目模板删除
     *
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataTypeClass = String.class, name = "id", value = "主键")
    })
    @ApiOperation(value = "题目库-题目模板删除", notes = "题目库-题目模板删除", httpMethod = "DELETE")
    @DeleteMapping("/{id:[a-fA-F0-9]{24}}")
    public XmResultJson<DeletedCountVo> questionlibDelete(@PathVariable String id) {
        return XmResultJson.success(questionlibService.questionlibDelete(id));
    }
}
